﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using DevWeb.Biztalk.DeploymentSettings.Data.CodeFirst;
using DevWeb.Biztalk.DeploymentSettings.WebUI.Models;

namespace DevWeb.Biztalk.DeploymentSettings.WebUI.Controllers
{
    public class TargetEnvironmentController : BaseController
    {
        //
        // GET: /TargetEnvironment/

        public ActionResult Index()
        {
            var environments = Context.TargetEnvironments.OrderBy(p=>p.Name).ToList();

            var model = Mapper.Map<List<TargetEnvironmentViewModel>>(environments);

            return View(model);
        }

       

        //
        // GET: /TargetEnvironment/Create

        public ActionResult Create()
        {

            var model = new TargetEnvironmentViewModel() {CreateDateTime = DateTime.Now};

            return View(model);
        }

        //
        // POST: /TargetEnvironment/Create

        [HttpPost]
        public ActionResult Create(TargetEnvironmentViewModel viewmodel)
        {
            try
            {
                var environment = Mapper.Map<TargetEnvironment>(viewmodel);
                Context.TargetEnvironments.Add(environment);
                 Context.SaveChanges(System.Web.HttpContext.Current.User.Identity.Name);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TargetEnvironment/Edit/5

        public ActionResult Edit(int id)
        {

            var environment = Context.TargetEnvironments.SingleOrDefault(p => p.Id.Equals(id));

            var model = Mapper.Map<TargetEnvironmentViewModel>(environment);
            return View(model);
        }

        //
        // POST: /TargetEnvironment/Edit/5

        [HttpPost]
        public ActionResult Edit(TargetEnvironmentViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    var environment = Context.TargetEnvironments.SingleOrDefault(p => p.Id.Equals(model.Id));
                    Mapper.Map(model, environment);
                     Context.SaveChanges(System.Web.HttpContext.Current.User.Identity.Name);

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
            else
            {
                return View(model);
            }
        }

        //
        // GET: /TargetEnvironment/Delete/5

        public ActionResult Delete(int id)
        {
            var environment = Context.TargetEnvironments.SingleOrDefault(p => p.Id.Equals(id));

            var model = Mapper.Map<TargetEnvironmentViewModel>(environment);

            return View(model);
        }

        //
        // POST: /TargetEnvironment/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var environment = Context.TargetEnvironments.SingleOrDefault(p => p.Id.Equals(id));

                Context.TargetEnvironments.Remove(environment);
                 Context.SaveChanges(System.Web.HttpContext.Current.User.Identity.Name);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
